import android.content.Context
import android.graphics.Bitmap
import android.graphics.Typeface
import android.text.Spannable
import android.text.SpannableString
import android.text.style.ForegroundColorSpan
import android.text.style.RelativeSizeSpan
import android.text.style.StyleSpan
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition
import com.yoomiito.app.R
import com.yoomiito.app.formatNumWithOutAdd
import com.yoomiito.app.model.bean.GoodsDetail
import com.yoomiito.app.model.sdd.ShareMsg
import com.yoomiito.app.utils.*
import de.hdodenhof.circleimageview.CircleImageView

class SddShareView(val context: Context) {
    val view = LayoutInflater.from(context).inflate(R.layout.sdd_share_ad_1, null, false)
    val userPhoto = view.findViewById<CircleImageView>(R.id.userPhoto)
    val userName = view.findViewById<TextView>(R.id.userName)
    val goodsPic = view.findViewById<ImageView>(R.id.goodsPic)
    val goodsName = view.findViewById<TextView>(R.id.goodsName)
    val size = view.findViewById<TextView>(R.id.size)
    val price = view.findViewById<TextView>(R.id.price)
    val qrCode = view.findViewById<ImageView>(R.id.QRCode)
    var userPhotoSuccess = false
    var goodsPicSuccess = false
    var qrCodeSuccess = false
    var makeViewListener: (View, Bitmap) -> Unit = { _, _ -> }

    fun setViewData(goodsDetail: GoodsDetail, msg: ShareMsg) {
        userName.text = UserInfoConfig.getUser().name
        goodsName.text = goodsDetail.title
        size.text = "${goodsDetail.volume.toString().formatNumWithOutAdd()}人已领取"
        val price0 = "0"
        val oldPrice = SpannableUtils.handlePrice(goodsDetail.reserve_price)
        val b = SpannableUtils.isEmptyOr0(oldPrice)
        val p = if (b) "¥${price0}" else "¥${price0} ¥${oldPrice}"
        val spanText = SpannableString(p)
        spanText.setSpan(RelativeSizeSpan(0.5f), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        if (!b){
            spanText.setSpan(RelativeSizeSpan(0.5f), price0.length+1, p.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
            spanText.setSpan(ForegroundColorSpan(ResUtil.getColor(R.color.color_balk_333333)), price0.length+1, p.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
            spanText.setSpan(StyleSpan(Typeface.BOLD), price0.length+2, p.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        }

        price.text=spanText
        Glide.with(context).asBitmap().load(UserInfoConfig.getUser().headimgurl).into(object : SimpleTarget<Bitmap>() {
            override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                userPhoto.setImageBitmap(resource)
                userPhotoSuccess = true
                makePic()
            }
        })
//            val corners = RoundedCorners(12)
//            val options = RequestOptions.bitmapTransform(corners)
        //.apply(options)
        Glide.with(context).asBitmap().load(goodsDetail.pict_url).into(object : SimpleTarget<Bitmap>() {
            override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                goodsPic.setImageBitmap(resource)
                goodsPicSuccess = true
                makePic()
            }
        })
        Glide.with(context).asBitmap().load(msg.url).into(object : SimpleTarget<Bitmap>() {
            override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                qrCode.setImageBitmap(resource)
                qrCodeSuccess = true
                makePic()
            }
        })

    }

    private fun makePic() {
        ImageUtils.layoutView(view, DensityUtil.dp2px(375f), DensityUtil.dp2px(620f))
        if (userPhotoSuccess && goodsPicSuccess && qrCodeSuccess) {
            makeViewListener(view, ImageUtils.viewSaveToImage(view))
        }
    }
}